use config::{self, File, FileFormat};
use serde::Deserialize;
use std::{path::Path, sync::OnceLock};

/// 配置文件
#[derive(Debug, Deserialize)]
pub struct AppConfig {
    /// ffmpeg文件的路径，如果为空，则会从系统环境中寻找
    ffmpeg_path: String,
    /// 语音转文本的模型路径
    whisper_model_path: String,
    /// 翻译地址URL
    libretranslate_url: String,
     /// 翻译地址APIKEY,不需要则留空
    libretranslate_api_key: String,
    /// 翻译目标语言
    libretranslate_target_lang: String,
}
static APP_CONFIG: OnceLock<AppConfig> = OnceLock::new();
impl AppConfig {
    pub fn get() -> &'static AppConfig {
        APP_CONFIG.get_or_init(|| {
            let mut conf_builder = config::Config::builder()
                .add_source(File::new("config/appconfig.json", FileFormat::Json));
            // if in debug mode
            if cfg!(debug_assertions){
             let devel_file=   Path::new("config/appconfig-develop.json");
                if devel_file.exists(){
                    conf_builder=conf_builder.add_source(config::File::new(devel_file.to_str().unwrap(), FileFormat::Json));
                }
            }
            let conf = conf_builder.build().expect("读取配置文件错误");
            conf.try_deserialize().expect("配置文件解析错误")
        })
    }
    pub fn ffmpeg_path(&self) -> &str {
        &self.ffmpeg_path
    }
    pub fn whisper_model_path(&self) -> &str {
        &self.whisper_model_path
    }
    pub fn libretranslate_url(&self) -> &str {
        &self.libretranslate_url
    }
    pub fn libretranslate_api_key(&self) -> &str {
        &self.libretranslate_api_key
    }
    pub fn libretranslate_target_lang(&self) -> &str {
        &self.libretranslate_target_lang
    }
}
